package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.InventoryUploadDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 同步全球sku库存
 * <p>
 * cy.inventory.upload.byAll
 * /product/202309/global_products/{global_product_id}/inventory/update （全球商品更新库存）https://partner.tiktokshop.com/docv2/page/650a11d0defece02be5ec7a6?external_id=650a11d0defece02be5ec7a6
 * </p>
 *
 * @author gywang
 * @since 2024-07-16
 */
@Slf4j
@Service
public class TiktokGlobalInventoryUploadByIdService extends AbstractBaseTiktokApiHandler<InventoryUploadDTO> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.GLOBAL_SKU_SYNCSTOCK;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.INVENTORY_UPLOAD_BY_ALL;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, InventoryUploadDTO uploadStoreInventoryDTO) {
        String url = getDefaultUrl(platformApiParamDTO.getStoreConfig());
        url = StringUtils.replace(url, "{global_product_id}", uploadStoreInventoryDTO.getPlatformSpuId());
        Map<String, Object> body = new HashMap<>();
        List<Map<String, Object>> global_skus = new ArrayList<>();
        body.put("global_skus", global_skus);
        Map<String, Object> sku = new HashMap<>();
        global_skus.add(sku);
        sku.put("id", uploadStoreInventoryDTO.getPlatformSkuId());
        Map<String, Object> inventory = new HashMap<>();
        List<Map<String, Object>> inventories = new ArrayList<>();
        inventories.add(inventory);
        sku.put("inventory", inventories);
        inventory.put("global_warehouse_id", uploadStoreInventoryDTO.getWarehouseCode());
        inventory.put("quantity", uploadStoreInventoryDTO.getSyncQty());
        return this.executeTiktokApi(url, platformApiParamDTO.getStoreConfig(), new HashMap<>(), JacksonTool.toJson(body), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
    }
}
